package com.relytosoft.mizar.analyze.algorithm.base.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.mizar.analyze.algorithm.base.service.FeatureService;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.FeatureComparisonReqDto;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.FeatureExtractionReqDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 图片特征值
 *
 * @Author SunLi
 * @Date: 2022/5/16 16:42
 * @Version 1.0
 */
@RestController
@RequestMapping("/algo/feature")
public class FeatureBaseController {

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private FeatureService featureService;

    @PostMapping(value = "/extraction")
    public ResponseResult extraction(@RequestBody String bodys) {

        FeatureExtractionReqDto featureExtractionReqDto = JSON.parseObject(bodys, FeatureExtractionReqDto.class);

        //参数验证
        if (featureExtractionReqDto == null || StrUtil.isEmpty(featureExtractionReqDto.getImageData())) {
            return ResponseResult.fail("参数异常！");
        }
        String result = featureService.featureExtraction(featureExtractionReqDto);
        log.info("图片分析结果：{}", result);
        return ResponseResult.ok(JSON.parseObject(result));
    }

    @PostMapping(value = "/comparison")
    public ResponseResult comparison(@RequestBody String bodys) {

        FeatureComparisonReqDto featureComparisonReqDto = JSON.parseObject(bodys, FeatureComparisonReqDto.class);

        //参数验证
        if (featureComparisonReqDto == null || StrUtil.isEmpty(featureComparisonReqDto.getImageData1()) || StrUtil.isEmpty(featureComparisonReqDto.getImageData2())) {
            return ResponseResult.fail("参数异常！");
        }

        Double result = featureService.featureComparison(featureComparisonReqDto);
        log.info("图片比对结果：{}", result);
        return ResponseResult.ok(result);
    }
}
